var bakOptions = "" //H5页面返回的参数
Page({
  data: {
    kefushow: false
  },
  //关闭支付弹窗
  closePopUp: function() {
    var that = this;
    that.setData({
      kefushow: false
    })
    that.setData({
      weburl: decodeURIComponent(bakOptions.returnUrl) //H5页面返回的参数
    })
  },
  //长按支付后的图片弹出保存图片
  viewImage(e) {
    const {
      src
    } = e.target.dataset;
    wx.previewImage({
      urls: [src],
    });
  },
  onLoad: function(options) {
    console.log(JSON.stringify(options))
    var that = this;
    that.requestPayment(options);
  },

  //根据 obj 的参数请求wx 支付
  requestPayment: function(options) {
    bakOptions = options;
    var that = this;
    var datas = {
      "body": options.title,
      "type": "1",
      "totalPay": options.price,
      "userId": wx.getStorageSync("userId")
    }

    getApp().requestLoading(getApp().globalData.https + "orderResource/order",
      "post", datas, "加载中...",
      function(res) {
        var r = res.data
        if (res.code == 0) {
          wx.requestPayment({
            'timeStamp': r.timeStamp,
            'nonceStr': r.nonceStr,
            'package': "prepay_id=" + r.package,
            'signType': 'MD5',
            'paySign': r.paySign,
            'success': function(res) {

              //   that.setData({
              //     flg: "0",
              //     msg: "支付成功！",
              //   })
              var channel = options.channel == undefined ? "" : options.channel;
              //调用通知第三方 H5
              var parm = {
                "orderNum": r.tradeNo,
                "extContent": options.orderId,
                "orderStatus": "1",
                "money": options.price,
                "payTime": r.timeStamp,
                "channel": channel,
                "name": options.title,
                "userId": wx.getStorageSync("userId")
              }
              //   console.log(parm)
              getApp().request(getApp().globalData.https + "weixinResource/pay/toSign",
                "post", parm,
                function(res) {
                  var data = res[0];
                  if (data.status == "0") {
                    //支付成功后》调用接口通知对方支付完成订单，处理完成后跳转
                    // that.setData({
                    //   weburl: decodeURIComponent(options.returnUrl)
                    // })
                    getApp().request(getApp().globalData.https + "resourcesResource/payWaiterWXPic",
                      "get", '',
                      function(res) {
                        that.setData({
                          kefubg: getApp().globalData.source + "dialog/kefu.png",
                          kefuerweim: res.data.location,
                          kefushow: true, //打开支付成功弹框
                        })
                      }
                    )
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: data.msg,
                      success: function(res) {
                        if (res.confirm) {
                          wx.navigateBack({
                            delta: 1 //想要返回的层级
                          })
                        } else if (res.cancel) {
                          wx.navigateBack({
                            delta: 1 //想要返回的层级
                          })
                        }
                      }
                    })
                  }

                }
              )

              // wx.showModal({
              // 	title: '提示',
              // 	content: '支付成功',
              // 	success: function (res) {

              // 		that.setData({
              // 			weburl: decodeURIComponent(options.returnUrl)
              // 		}) 
              // 		if (res.confirm) {
              // 			wx.navigateBack({
              // 				delta: 2//想要返回的层级
              // 			})
              // 		} else if (res.cancel) {
              // 			wx.navigateBack({
              // 				delta: 2//想要返回的层级
              // 			})
              // 		}
              // 	}
              // }) 


              //调用通知微信消息
              var parm = {
                "formId": "",
                "mark": "天天必听-支付通知",
                "name": options.title,
                "pageIndex": "pages/style/style",
                "price": options.price,
                "userId": wx.getStorageSync("userId")
              }
              getApp().request(getApp().globalData.https + "customer/purchase/notice",
                "post", parm,
                function(res) {
                  console.log("支付结果=" + res)
                }
              )
            },
            'fail': function(res) {
              // that.payCallback();
              //   that.setData({
              //     flg: "-1",
              //     msg: "支付失败！"
              //   })
              wx.showModal({
                title: '提示',
                content: '支付失败',
                success: function(res) {
                  if (res.confirm) {
                    wx.navigateBack({
                      delta: 1 //想要返回的层级
                    })
                  } else if (res.cancel) {
                    wx.navigateBack({
                      delta: 1 //想要返回的层级
                    })
                  }
                }
              })
            }
          })
        }
      });

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
  },
})